import random
import pygame

SCREEN_RECT=pygame.Rect(0,0,480,700)
#刷新帧率
FRAME_PER_SEC=60

screen=pygame.display.set_mode(SCREEN_RECT.size)
class PlaneSprite(pygame.sprite.Sprite):

    def __init__(self,image_name,speed=1):
        super().__init__()
        self.image=pygame.image.load(image_name)
        self.rect=self.image.get_rect()
        self.speed=speed

    def update(self, *args):
        self.rect.y+=self.speed


class BackGround(PlaneSprite):
    def update(self, *args):
        super().update(*args)
        if self.rect.y>SCREEN_RECT.height:
            self.rect.y=-self.image.get_height()

class Enemy(PlaneSprite):
    def __init__(self):
        super().__init__("./images/enemy1.png")
        self.speed=random.randint(1,3)
        self.rect.x=random.randint(0,SCREEN_RECT.width-self.rect.width)
        self.rect.bottom=0
        self.total_HP=100
        self.cur_HP=100
        pygame.font.init()
        font_list = pygame.font.get_fonts()
        # font_name = pygame.font.get_default_font()
        self.font = pygame.font.Font(pygame.font.match_font(font_list[0]), 22)
        self.bullets=pygame.sprite.Group()

    def update(self, *args):
        super().update()

        if self.rect.y>SCREEN_RECT.height:
            self.kill()

    def fire(self):
        bullet = Bullet("./images/bullet1.png")
        bullet.rect.centerx = self.rect.centerx
        bullet.rect.top = self.rect.bottom
        bullet.speed=self.speed+1
        self.bullets.add(bullet)

    def show_HP(self):
        image = self.font.render("ENEMY_HP:%d / %d" % (self.cur_HP, self.total_HP), True, [0, 0, 0], [255, 255, 255])
        screen.blit(image, (20, 60))


    def __del__(self):
        #print("敌军销毁")
        e=pygame.event.Event(22,{"enemy":self})
        pygame.event.post(e)

class Hero(PlaneSprite):
    def __init__(self):
        super().__init__("./images/me2.png")
        pygame.time.set_timer(21,500)
        self.rect.centerx=SCREEN_RECT.width/2
        self.rect.bottom=SCREEN_RECT.height
        self.speed=0
        self.total_HP = 500
        self.cur_HP = 500
        pygame.font.init()
        font_list =pygame.font.get_fonts()
        #font_name = pygame.font.get_default_font()
        self.font = pygame.font.Font(pygame.font.match_font(font_list[0]), 22)
        self.bullets=pygame.sprite.Group()

    def update(self, *args):
        self.rect.x+=self.speed

        if self.rect.x<0:
            self.rect.x=0
        elif self.rect.right>SCREEN_RECT.width:
            self.rect.right=SCREEN_RECT.width
        elif self.rect.y<0:
            self.rect.y=0
        elif self.rect.bottom>SCREEN_RECT.height:
            self.rect.bottom=SCREEN_RECT.height

    #发射子弹
    def fire(self):
        for i in (0,1,2):
            bullet = Bullet("./images/bullet2.png")
            bullet.rect.centerx = self.rect.centerx
            bullet.rect.bottom = self.rect.y-i*20
            self.bullets.add(bullet)

    #显示英雄血条
    def show_HP(self):
        image=self.font.render("HERO_HP:%d / %d" % (self.cur_HP,self.total_HP),True,[0, 0, 0], [255, 255, 255])
        screen.blit(image,(20,20))

    def __del__(self):
        #print("英雄销毁")
        pass



class Bullet(PlaneSprite):
    def __init__(self,image_name):
        super().__init__(image_name)
        self.speed=-3

    def update(self, *args):
        super().update()
        if self.rect.y<0:
            self.kill()

    def __del__(self):
        #print("子弹精灵被销毁")
        pass